"""
Case Type   : ubtree页面解析工具
Case Name   : pagehack 解析hash索引， -t 为ubtree索引类型，合理报错
Create At   : 2025-10-28
Owner       : sungang14
Description :
    1、创建表
    2、创建hash索引
    3、批量插入1000条测试数据
    4、查询索引文件路径
    5、-t ubtree_pcr_index和-t ubtree_index解析索引
Expect      :
    1、设置成功
    2、成功
    3、成功
    4、成功
    5、解析索引页面失败，合理报错--'
History     :
"""

import os
import time
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

logger = Logger()


class SlowSQLTestCase(unittest.TestCase):
    def setUp(self):
        logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.userNode = Node('PrimaryDbUser')
        self.table_name = f"t_ubtree_pagehack_0030"
        self.index_name = f"i_ubtree_pagehack_0030"
        self.prepare_data_00 = f"drop table if exists {self.table_name} cascade;"
        self.prepare_data_01 = f"""create table {self.table_name} (
                                    id serial primary key,
                                    name varchar(50),
                                    description text,
                                    points point,
                                    tags text[],
                                    score double precision,
                                    created_at timestamp default current_timestamp
                                    ) with (orientation=row);"""
        self.prepare_data_02 = f"create index {self.index_name} on {self.table_name} using hash(description);"
        self.prepare_data_03 = f"insert into {self.table_name} (name, description, points, tags, score) select 'product_' || n::text, " \
                               f"'description for product ' || n::text, point(random() * 100, random() * 100), array[('tag' || (floor(random() * 10)::int)::text), " \
                               f"('category' || (floor(random() * 5)::int)::text)], random() * 100 from generate_series(1, 1000) as n;"
        self.prepare_data_04 = f"SELECT pg_relation_filepath('{self.index_name}');"

    def test_slowsql(self):
        text = '--step1:创建表;expect:成功--'
        logger.info(text)
        data_cmd = self.comsh.execut_db_sql(self.prepare_data_00)
        logger.info(data_cmd)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, data_cmd, '执行失败' + text)
        data_cmd = self.comsh.execut_db_sql(self.prepare_data_01)
        logger.info(data_cmd)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, data_cmd, '执行失败' + text)

        text = '--step2:创建hash索引;expect:成功--'
        logger.info(text)
        data_cmd = self.comsh.execut_db_sql(self.prepare_data_02)
        logger.info(data_cmd)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS_MSG, data_cmd, '执行失败' + text)

        text = '--step3:批量插入1000条测试数据;expect:成功--'
        logger.info(text)
        data_cmd = self.comsh.execut_db_sql(self.prepare_data_03)
        logger.info(data_cmd)
        self.assertIn(f"INSERT 0 1000", data_cmd, '执行失败' + text)

        text = '--step4:查询索引文件路径;expect:成功--'
        logger.info(text)
        time.sleep(5)
        data_cmd = self.comsh.execut_db_sql(self.prepare_data_04)
        logger.info(data_cmd)
        self.assertIn(f"1 row", data_cmd, '执行失败' + text)
        res_path = data_cmd.splitlines()[-2].strip()
        logger.info(res_path)

        text = '--step5:-t ubtree_pcr_index和-t ubtree_index解析索引;expect:解析索引页面失败，合理报错--'
        logger.info(text)
        ph_cmd = f"source {macro.DB_ENV_PATH}; cd $PGDATA; pagehack -f {res_path} -t ubtree_pcr_index"
        logger.info(ph_cmd)
        res = self.userNode.sh(ph_cmd).result()
        logger.info(res)
        self.assertIn(f'it is not ubtree index file, please choose correct type', res, '执行失败' + text)
        ph_cmd = f"source {macro.DB_ENV_PATH}; cd $PGDATA; pagehack -f {res_path} -t ubtree_index"
        logger.info(ph_cmd)
        res = self.userNode.sh(ph_cmd).result()
        logger.info(res)
        self.assertIn(f'it is not ubtree index file, please choose correct type', res, '执行失败' + text)

    def tearDown(self):
        text = '--step6:清理环境，恢复默认值;expect:成功--'
        logger.info(text)
        data_cmd = self.comsh.execut_db_sql(self.prepare_data_00)
        logger.info(data_cmd)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, data_cmd, '执行失败' + text)
        logger.info(f'-----{os.path.basename(__file__)} end-----')
